﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace EFCoreTest
{
    public class reflectionContext:DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=192.168.3.109;Initial Catalog=text;User ID=sa;Password=123456");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var Assembly = this.GetType().Assembly;
            var Types = Assembly.GetTypes().Where(m =>
                                                       m.FullName != null
                                                       && typeof(EntityBase).IsAssignableFrom(m)
                                                       && !m.IsAbstract
                                                       && m.Name != "EntityBase").ToArray(); ;
            foreach (var entityType in Types)
            {
                modelBuilder.Entity(entityType);
            }
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly);
            base.OnModelCreating(modelBuilder);
        }
    }
}
